package com.itx.gatewayx.common.model;

import com.itx.gatewayx.common.constants.SystemConstants;
import lombok.Data;

import java.io.Serializable;

/**
 * 响应结果
 *
 * @author itx
 */
@Data
public class ResponseResult<T> implements Serializable {

    private static final long serialVersionUID = 1L;

    /**
     * 成功
     */
    public static final int SUCCESS = SystemConstants.SUCCESS_CODE;

    /**
     * 失败
     */
    public static final int ERROR = SystemConstants.ERROR_CODE;

    /**
     * 状态码
     */
    private int code;

    /**
     * 返回信息
     */
    private String msg;

    /**
     * 数据对象
     */
    private T data;

    public static <T> ResponseResult<T> success() {
        return restResult(null, SUCCESS, "操作成功");
    }

    public static <T> ResponseResult<T> success(T data) {
        return restResult(data, SUCCESS, "操作成功");
    }

    public static <T> ResponseResult<T> success(String msg) {
        return restResult(null, SUCCESS, msg);
    }

    public static <T> ResponseResult<T> success(String msg, T data) {
        return restResult(data, SUCCESS, msg);
    }

    public static <T> ResponseResult<T> error() {
        return restResult(null, ERROR, "操作失败");
    }

    public static <T> ResponseResult<T> error(String msg) {
        return restResult(null, ERROR, msg);
    }

    public static <T> ResponseResult<T> error(T data) {
        return restResult(data, ERROR, "操作失败");
    }

    public static <T> ResponseResult<T> error(String msg, T data) {
        return restResult(data, ERROR, msg);
    }

    public static <T> ResponseResult<T> error(int code, String msg) {
        return restResult(null, code, msg);
    }

    private static <T> ResponseResult<T> restResult(T data, int code, String msg) {
        ResponseResult<T> r = new ResponseResult<>();
        r.setCode(code);
        r.setData(data);
        r.setMsg(msg);
        return r;
    }

    public static <T> Boolean isSuccess(ResponseResult<T> responseResult) {
        return responseResult != null && responseResult.getCode() == SUCCESS;
    }

    public static <T> Boolean isError(ResponseResult<T> responseResult) {
        return !isSuccess(responseResult);
    }
} 